package com.njrsrc.android.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 取消收藏/收藏/申请工作
 * 
 * @author Administrator
 * 
 */
public class CancelOrCollectOrApplyTask extends AsyncTask<Object, Integer, Void> {
	private String url;
	private Handler mHandler;
	private UserInfo userInfo;

	@Override
	protected Void doInBackground(Object... params) {
		if (params.length < 4) {
			return null;
		}
		mHandler = (Handler) params[0];
		url = (String) params[1];
		userInfo = (UserInfo) params[2];
		String type = (String) params[3];
		JSONObject jsonObject = Helpers.getJSONArrayByGet(url);
		Message msg = mHandler.obtainMessage();
		// 1：加入收藏；2：取消收藏；3：申请工作
		try {
			if ("1".equals(type)) {
				if (jsonObject == null) {
					msg.what = MsgID.JOB_ADD_COLLECT_ERROR;
				} else if (jsonObject.toString().contains("error_code")) {
					// 返回错误
					userInfo.setError_code(jsonObject.getString("error_code"));
					userInfo.setError_message(jsonObject.getString("error_message"));
					msg.what = MsgID.JOB_ADD_COLLECT_FAIL;
				} else {
					msg.what = MsgID.JOB_ADD_COLLECT_SUCCESS;
				}
			} else if ("2".equals(type)) {
				if (jsonObject == null) {
					msg.what = MsgID.JOB_CANCEL_COLLECT_ERROR;
				} else if (jsonObject.toString().contains("error_code")) {
					// 返回错误
					userInfo.setError_code(jsonObject.getString("error_code"));
					userInfo.setError_message(jsonObject.getString("error_message"));
					msg.what = MsgID.JOB_CANCEL_COLLECT_FAIL;
				} else {
					msg.what = MsgID.JOB_CANCEL_COLLECT_SUCCESS;
				}
			} else {
				if (jsonObject == null) {
					msg.what = MsgID.JOB_APPLY_ERROR;
				} else if (jsonObject.toString().contains("error_code")) {
					// 返回错误
					userInfo.setError_code(jsonObject.getString("error_code"));
					userInfo.setError_message(jsonObject.getString("error_message"));
					msg.what = MsgID.JOB_APPLY_FAIL;
				} else {
					msg.what = MsgID.JOB_APPLY_SUCCESS;
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mHandler.sendMessage(msg);
		return null;
	}
}